function TrimContLength(cont, max_len) {
  if (cont.length > max_len)
    cont = cont.substring(0, max_len - 1) + '......';
  return cont;
}

function generate_list_cont(list_col_name_arr) {
  var search_list_thead='';
  var result_item_img_div = '<div class="SearchResultDivImgClass">' + TrimContLength(list_col_name_arr[0], 300) + '</div>';
  var result_info_group_name_div = '<div class="SearchResultGroupNameClass">' + TrimContLength(list_col_name_arr[1], 300) + '</div>';
  var result_info_group_des_div = '<div class="SearchResultGroupDesClass">' + TrimContLength(list_col_name_arr[2], 150) + '</div>';
  var result_info_group_detail_div = '<div class="SearchResultGroupDetailDivClass">' + result_info_group_des_div + '</div>';
  var result_info_group_location_div = '<div>主要活动地点：<strong>' + list_col_name_arr[3] + '</strong></div>';
  var result_info_group_mem_count_div = '<div>成员数目：<strong>' + list_col_name_arr[5] + '</strong></div>';
  var result_info_group_event_count_div = '<div>已举办的活动数：<strong>' + list_col_name_arr[6] + '</strong></div>';
  var result_info_group_foundeddate_div = '<div>成立时间：' + list_col_name_arr[4] + '</div>'; 
  var result_info_group_next_meetup_div = '';
  if (list_col_name_arr[7] != "")
    result_info_group_next_meetup_div = '<div><span>下次活动时间:</span>' + list_col_name_arr[7] + '</div>';
  var result_info_group_others_info_div = '<div class="SearchResultOthersInfoDivClass">' + 
    result_info_group_mem_count_div + result_info_group_event_count_div + result_info_group_location_div + result_info_group_foundeddate_div + result_info_group_next_meetup_div + '</div>';
  search_list_thead = "<li class='SearchResultLiClass'>" + result_item_img_div + 
    "<div class='SearchResultOuterDivClass'>" + result_info_group_name_div + result_info_group_others_info_div + result_info_group_detail_div + + "</div></li>";
  return search_list_thead;
};

function generate_head_cont(list_col_name_arr, list_col_size_arr, thclass_arr) {
  var search_list_thead='';
  for (var i = 0; i < list_col_name_arr.length; ++i) {
    var th = '<div style="width:'
      + list_col_size_arr[i] + '" '
      + 'class="' + thclass_arr[i] + '">'
      + list_col_name_arr[i] + '</div>';
    search_list_thead += th;
  }
  return search_list_thead;
};

function LoadSearchingResult() {
  $.ajax({
    url: searchresult_url + '/?' + GetUrlParams(),
    type: 'GET',
    dataType: 'text',
    timeout: 10000,
    error: function(){
      alert('Error loading XML document');
    },
    success: function(HTML){
      var search_result_num = $(HTML).find('.searchresult').size();
      var search_result_name = $(HTML).find('.searchresultname').text();
      var search_keywords = $(HTML).find('.searchkeywords').text();
      
      // Add search title.
      var search_result_title;
      if (search_keywords)
        search_result_title = '<h2>'
                                + search_result_num
                                + ' ' + search_result_name
                                + ' 匹配'
                                + '关键词 "' + search_keywords
                                + '"</h2>';
      else
        search_result_title = '<h2>' + search_result_num + '个碰头吧小组</h2>';
      var bar_line = '<div style="border-bottom:2px solid #ccc; height:7px"></div>';
      $(search_result_title).appendTo('#SearchResultTitle');
      $(bar_line).appendTo('#SearchResultTitle');
      if (search_result_num > 0)
      {
        // Add seatch list table column name.
        // var search_result_list_head = generate_head_cont(
        //  list_col_name, list_col_size,
        //  ['SearchResultLHeadclass','SearchResultLHeadclass','SearchResultLHeadclass']);
        // $(search_result_list_head).appendTo('#SearchResultListHead');

        $(HTML).find('.searchresult').each(function() {
          // Add search list items.
          var item_arr = new Array();
          item_arr.push($(this).find('.groupPicUrl').html());
          item_arr.push($(this).find('.groupName').html());
          item_arr.push($(this).find('.groupDes').html());
          item_arr.push($(this).find('.groupLocation').html());
          item_arr.push($(this).find('.groupFoundedDate').html());
          item_arr.push($(this).find('.groupMemCount').html());
          item_arr.push($(this).find('.groupEventCount').html());
          item_arr.push($(this).find('.groupNextMeetup').html());

          var one_row_to_search_list = generate_list_cont(item_arr);
          $(one_row_to_search_list).appendTo('#SearchResultListItemsUl');
          });
			//added by  nemo 12-27: for showing the chinese characters for DOW.
		  $('.dow').each(function(){
			this.textContent = chineseDoW[parseInt(this.textContent)];
			});
      }
    }
  });
}

function ClickFired() {
  $('#search_result_block').hide('fast');
  $('#SearchResultTitle').empty();
  $('#SearchResultListHead').empty();
  $('#SearchResultListItemsUl').empty();
  $('#search_result_block').show('fast');
  LoadSearchingResult();
}

$(function() {
  $('#search_result_block').css('display', 'none');
  $('#search_group').click(function(){
    ClickFired();
    });
  ClickFired();
});
